package code.whmmm.util.simple;

import java.util.Arrays;
import java.util.Collection;

/**
 * <p><b>
 * 字符串工具类
 * </b></p>
 * <br/>
 * 创建时间: 8/28/2019 8:22 AM
 *
 * @author Zero
 */
public class StrUtil {
    private StrUtil() {

    }

    /**
     * 连接字符串。<br/>
     * 不包含 空字符，null
     *
     * @param separator  分割符号
     * @param collection 集合
     * @return 字符串
     */
    public static String join(CharSequence separator, Collection<String> collection) {
        if (separator == null || collection == null) {
            throw new RuntimeException("参数不能包含null");
        }
        StringBuilder sb = new StringBuilder();

        for (String next : collection) {
            if (next != null && !"".equals(next.trim())) {
                if (sb.length() != 0) {
                    sb.append(separator);
                }
                sb.append(next.trim());
            }
        }

        return sb.toString();
    }

    /**
     * @see StrUtil#join(CharSequence, Collection)
     */
    public static String join(char c, Collection<String> collection) {
        return join(Character.toString(c), collection);
    }

    /**
     * @see StrUtil#join(CharSequence, Collection)
     */
    public static String join(char c, String[] strings) {
        return join(Character.toString(c), Arrays.asList(strings));
    }

    /**
     * @see StrUtil#join(CharSequence, Collection)
     */
    public static String join(CharSequence separator, String[] strings) {
        return join(separator, Arrays.asList(strings));
    }


    /**
     * 判断字符串是否是空字符串
     * <pre>{@code
     *  //example
     *  "" -> true
     *  null -> true
     *  "     " -> true
     *  "abc" -> false
     *  " abc " -> false
     * }</pre>
     *
     * @param str 要判断字符串
     * @return {@link Boolean}
     */
    public static boolean isBlank(String str) {
        return null == str || "".equalsIgnoreCase(str.trim());
    }

    /**
     * 字符串 不为空 返回 true
     *
     * @param str
     * @return
     */
    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }


    /**
     * 0130100 -> '0130100'
     *
     * @param v 目标
     * @return 字符串
     */
    public static String wrapSqlStr(Object v) {
        return "'" + ValueWrapper.getString(v) + "'";
    }

    /**
     * 0130100 -> '0130100'
     *
     * @param v 目标
     * @return 字符串
     */
    public static String wrapSqlStr(String v) {
        return "'" + v + "'";
    }
}
